﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data;

public partial class MRP_DeliveryBillSignOff : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void UIServer1_CmdFieldFire1(object sender, Evolutility.UIServer.CmdFieldEventArgs e)
    {
        int id = Int32.Parse(e.PK);

        StringBuilder sbSQL = new StringBuilder();


        if (e.CommandText.Equals("发布"))
        {
            Database db = DatabaseFactory.CreateDatabase("MRPSqlServer");

            string sSQL = string.Format("Select [ProductID],[OrderID],[SignOffQuantity] from [MRP_DeliveryItem] where ID={0}",id);

            using (IDataReader reader = db.ExecuteReader(CommandType.Text, sSQL))
            {
                if (reader.Read())
                {
                    sbSQL.AppendFormat("Update [MRP_OrderDetail] set [SignedOffQty]=[SignedOffQty]+{0} where ProductID={1} and OrderID={2};",reader[2],reader[0],reader[1]);
                    sbSQL.AppendFormat("Update [MRP_OrderDetail] set [IsComplete]=1 where [SignedOffQty]>=[Quantity] and ProductID={0} and OrderID={1};", reader[0], reader[1]);
                    sbSQL.AppendFormat("Update [MRP_Order] set [Status]=3 where not exists(Select * from MRP_OrderDetail where ID={0} and IsComplete=0) and ID={0};", reader[1]);
                    sbSQL.AppendFormat("Update [MRP_Stock] Set InLock=InLock-{0} where MaterialID={1};",reader[2],reader[0]);
                    sbSQL.AppendFormat(" Update dbo.MRP_DeliveryItem set Status=3 where ID = {0}", id);
                    MRPUtility.WriteLogForSQL(sbSQL.ToString());
                    db.ExecuteNonQuery(CommandType.Text, sbSQL.ToString());
                }
            }
        }
    }
}
